
/**
 * copyright(c) 2013 www.jiongshibaoliao.com, All Rights Reserved.
 * project: jiongshibaoliao 
 * author: 曾帅
 * date: 2014-1-5 下午06:14:56
 * version: v1.0
 */

package com.jiongshibaoliao.web;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiongshibaoliao.common.constants.ErrorCode;
import com.jiongshibaoliao.common.mybatis.Page;
import com.jiongshibaoliao.common.mybatis.PageBean;
import com.jiongshibaoliao.exception.JiongException;
import com.jiongshibaoliao.model.User;
import com.jiongshibaoliao.model.bean.UserBean;
import com.jiongshibaoliao.service.ArticleService;
import com.jiongshibaoliao.service.UserService;
import com.jiongshibaoliao.util.JiongMD5;
import com.jiongshibaoliao.util.VerifyCode;

/**
 * @project jiongshibaoliao
 * @author 曾帅 
 * @version 1.0
 * @date 2014-1-5 下午06:14:56   
 */

@Controller
public class UserController extends BaseController {

	private static final Logger logger = Logger.getLogger(UserController.class);
	
	@Autowired
	public ArticleService articleService;
	
	@Autowired
	public UserService userService;

	@RequestMapping(value = "/signup", method = { RequestMethod.GET, RequestMethod.POST })
	public String signup(HttpServletRequest request, ModelMap model) {
		
		return "signup";
	}
	
	@RequestMapping(value = "/isUserExist", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody JSONObject isUserExist(HttpServletRequest request, ModelMap model) {
		String loginName = String.valueOf(request.getParameter("loginName"));
		JSONObject returnJson = new JSONObject();
		if(userService.isUserExist(loginName)) {
			returnJson.put("code", ErrorCode.CODE_10006);
			return returnJson;
		}
		
		returnJson.put("code", ErrorCode.CODE_10000);
		return returnJson;
	}
	
	@RequestMapping(value = "/doSignup", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody JSONObject doSignup(@RequestBody String jsonStr, HttpServletRequest request, ModelMap model) throws JiongException {
		JSONObject json = JSONObject.fromObject(jsonStr);
		String loginName = String.valueOf(json.get("loginName"));
		String nickName = String.valueOf(json.get("nickName"));
		String passWord = String.valueOf(json.get("passWord"));
		String vcode = String.valueOf(json.get("vcode"));
		JSONObject returnJson = new JSONObject();
		if(StringUtils.isBlank(loginName) || StringUtils.isBlank(nickName) || StringUtils.isBlank(passWord) || StringUtils.isBlank(vcode)) {
			returnJson.put("code", ErrorCode.CODE_10001);
			return returnJson;
		}
		if((vcode.toLowerCase()).equals(String.valueOf(request.getSession().getAttribute(VerifyCode.VERIFY_CODE_KEY)).toLowerCase()) == false) {
			returnJson.put("code", ErrorCode.CODE_10003);
			return returnJson;
		}
		if(userService.isUserExist(loginName)) {
			returnJson.put("code", ErrorCode.CODE_10006);
			return returnJson;
		}
		
		User user = new User();
		user.setLoginName(loginName);
		user.setNickName(nickName);
		user.setPassWord(JiongMD5.toMD5(passWord));
		userService.create(user);
		
		UserBean userBean = new UserBean(user.getLoginName(), user.getNickName(), user.getEmail(), user.getMobile(), user.getBirthday(), user.getSex(), user.getHeadImgPath(), user.getUserType(), user.getActiveStatus(), user.getSingupSource(), user.getLastLoginDate(), user.getLoginTimes());
		request.getSession().setAttribute(UserBean.USER_SESSION_KEY, userBean);
		logger.info("user signup success, loginName=" + user.getLoginName());
		
		returnJson.put("code", ErrorCode.CODE_10000);
		return returnJson;
	}
	
	@RequestMapping(value = "/signupSuc", method = { RequestMethod.GET, RequestMethod.POST })
	public String signupSuc(HttpServletRequest request, ModelMap model) {
		
		return "signupSuc";
	}
	
	@RequestMapping(value = "/fetchpass", method = { RequestMethod.GET, RequestMethod.POST })
	public String fetchpass(HttpServletRequest request, ModelMap model) {

		return "fetchpass";
	}
	
	@RequestMapping(value = "/user/{id}", method = { RequestMethod.GET, RequestMethod.POST })
	public String user(@PathVariable("id") String id, PageBean pageBean, HttpServletRequest request, ModelMap model) {
		UserBean userBean = super.sessionUser(request);
		Page page = articleService.getArticlePageListByUserId(pageBean, userService.getUserIdByLoginName(userBean.getLoginName()));
		
		model.put("articleList", page.getlistResult());
		model.put("pageBean", page.getPageBean());

		return "user";
	}
	
	@RequestMapping(value = "/user/{id}/info", method = { RequestMethod.GET, RequestMethod.POST })
	public String userInfo(@PathVariable("id") String id, HttpServletRequest request, ModelMap model) {

		return "userInfo";
	}
}
